package inteface.utils;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;
import javax.swing.plaf.basic.BasicFileChooserUI;

/**
*  FileChange.
* 
*  @author Vincent GUIJARRO
*  @version 0.2, 24/03/06
*/
public class FileChange implements PropertyChangeListener {
	private JFileChooser fileChooser;
	private BasicFileChooserUI ui;
	private String nomfic;
	
	public FileChange(JFileChooser fileChooser, String nomfic) {
		this.fileChooser = fileChooser;
		this.nomfic = nomfic;
	}

	public void propertyChange(PropertyChangeEvent e) {
		String prop = e.getPropertyName();
		
		if (JFileChooser.FILE_FILTER_CHANGED_PROPERTY.equals(prop)) {
			FileFilter fv = fileChooser.getFileFilter();
			ui = (BasicFileChooserUI)fileChooser.getUI();
			String tmp = ui.getFileName();
			if ((tmp != null) && (tmp.length() != 0))
				nomfic = tmp;
			
			if (fv.getDescription().equals(TERImageFilter.PNG_image)) 
				nomfic = Utils.getNom(nomfic) + ".png";
			else
				if (fv.getDescription().equals(TERImageFilter.JPEG_image))
					nomfic = Utils.getNom(nomfic) + ".jpeg";
				else
					if (fv.getDescription().equals(TERImageFilter.BMP_image))
						nomfic = Utils.getNom(nomfic) + ".bmp";
					else
						if (fv.getDescription().equals(TERImageFilter.PS_image))
							nomfic = Utils.getNom(nomfic) + ".ps";
						else
							if (fv.getDescription().equals(Utils.SAV_Description))
								nomfic = Utils.getNom(nomfic) + ".sav";
			
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					ui.setFileName(nomfic);
				}
			});
			fileChooser.repaint();
		}
	}
	
	public void setNomFichier(String n) {
		this.nomfic = n;
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ui.setFileName(nomfic);
			}
		});
		fileChooser.repaint();
	}
}
